import 'package:flutter/material.dart';
import 'package:wallpaper/components/input/normal_input.dart';
import 'package:wallpaper/pages/other/search_pixiv1.dart';
import 'package:wallpaper/pages/other/search_pixiv2.dart';

class SearchImage extends StatefulWidget {
  const SearchImage({super.key});

  @override
  State<SearchImage> createState() => _SearchImageState();
}

class _SearchImageState extends State<SearchImage>
    with TickerProviderStateMixin {
  TextEditingController controller = TextEditingController();
  PageController pageController = PageController();
  late TabController tabController;
  bool change = true;

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 2, vsync: this);
  }

  @override
  void dispose() {
    controller.dispose();
    pageController.dispose();
    tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(''),
        toolbarHeight: 0,
        elevation: 0,
        leading: null,
        backgroundColor: Theme.of(context).colorScheme.primaryContainer,
        surfaceTintColor: Theme.of(context).colorScheme.primaryContainer,
      ),
      body: Column(
        children: [
          _buildSearch(),
          Container(
            height: 35,
            color: Theme.of(context).colorScheme.primaryContainer,
            child: TabBar(
              dividerColor: Colors.transparent,
              labelColor: Theme.of(context).colorScheme.primary,
              unselectedLabelColor: Theme.of(context).colorScheme.onSurface,
              controller: tabController,
              indicatorPadding: const EdgeInsets.all(2), // 调整指示器的内边距
              indicatorSize: TabBarIndicatorSize.label, // 使用标签的宽度
              tabs: [
                Tab(text: 'pixiv1'),
                Tab(text: 'pixiv2'),
              ],
              onTap: (value) {
                pageController.animateTo(
                  value * MediaQuery.of(context).size.width,
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeInOut,
                );
              },
            ),
          ),
          Expanded(
              child: PageView(
            controller: pageController,
            children: [
              SearchPixiv1(tag: controller.text, change: change),
              SearchPixiv2(tag: controller.text, change: change),
            ],
            onPageChanged: (value) {
              tabController.animateTo(value);
            },
          ))
        ],
      ),
    );
  }

  Widget _buildSearch() {
    return Container(
        padding: EdgeInsets.only(left: 10, right: 10, bottom: 2, top: 10),
        color: Theme.of(context).colorScheme.primaryContainer,
        child: Row(
          children: [
            Expanded(
              child: NormalInput(
                height: 35,
                icon: Icon(Icons.image_search_sharp),
                controller: controller,
                title: '关键词',
                onSubmitted: (v) {
                  FocusScope.of(context).requestFocus(FocusNode());
                  setState(() {
                    change = !change;
                  });
                },
              ),
            ),
            IconButton(
                onPressed: () {
                  FocusScope.of(context).requestFocus(FocusNode());
                  setState(() {
                    change = !change;
                  });
                },
                icon: Icon(Icons.search))
          ],
        ));
  }
}
